package main

func removeNthFromEnd(head *ListNode, n int) *ListNode {

	newList := &ListNode{Next: head}
	p, q := newList, newList

	for i := 0; i < n+1; i++ {
		if q != nil {
			q = q.Next
		}
	}
	for q != nil {
		p = p.Next
		q = q.Next
	}
	p.Next = p.Next.Next
	return newList.Next
}

func main() {

}

func removeNthFromEnd1(head *ListNode, n int) *ListNode {

	newList := &ListNode{Next: head}
	p, q := newList, newList
	for i := 0; i < n; i++ {
		if q != nil {
			q = q.Next
		}
	}
	for q.Next != nil {
		p = p.Next
		q = q.Next
	}
	p.Next = p.Next.Next
	return newList.Next
}
